package com.woniu.sharepark.platformsystem.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.service.UserService;
import com.woniu.sharepark.leasesystem.common.pojo.OrderForm;
import com.woniu.sharepark.leasesystem.common.service.OrderFormService;
import com.woniu.sharepark.leasesystem.lessor.pojo.CarPark;
import com.woniu.sharepark.leasesystem.lessor.service.CarParkService;
import com.woniu.sharepark.leasesystem.tenant.pojo.Car;
import com.woniu.sharepark.leasesystem.tenant.pojo.Complaint;
import com.woniu.sharepark.leasesystem.tenant.service.CarService;
import com.woniu.sharepark.platformsystem.log.annonation.BackgroundLogAnnotation;
import com.woniu.sharepark.platformsystem.service.ScaleService;
import com.woniu.sharepark.propertysystem.dto.OrderDTO;
import com.woniu.sharepark.leasesystem.tenant.service.ComplaintService;
import com.woniu.sharepark.leasesystem.tenant.vo.ComplaintVO;
import com.woniu.sharepark.propertysystem.pojo.Community;
import com.woniu.sharepark.propertysystem.service.CommunityService;
import com.woniu.sharepark.util.VerificationCodeUtil;
import com.woniu.sharepark.vo.LayuiDataVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 平台方控制层
 * @author xin
 * @date 2019/6/4 20:11
 * version v1.0
 */
@Controller
@RequestMapping("/platform")
public class PlatformController {

    @Resource
    private CarService carService;

    @Resource
    private CarParkService carParkService;

    @Resource
    private OrderFormService orderFormService;

    @Resource
    private ScaleService scaleService;

    @Resource
    private UserService userService;

    @Resource
    private ComplaintService complaintService;

    @Resource
    private CommunityService communityService;


    /**
     * 查询所有小区信息
     * @title findAllCommunity
     * @param currentPage 当前页
     * @param pageSize 每页数据
     * @return com.woniu.sharepark.vo.LayuiDataVO<com.woniu.sharepark.propertysystem.pojo.Community>
     * @author xin
     * @date 2019/6/4 20:17
     */
    @RequestMapping("/community")
    @ResponseBody
    public LayuiDataVO<Community> findAllCommunity(@RequestParam("page") Integer currentPage, @RequestParam("limit") Integer pageSize) {


        List<Community> list = communityService.findAllCommunity(currentPage, pageSize);
        PageInfo<Community> pageInfo = new PageInfo<>(list);


        // 设置返回的表格数据
        LayuiDataVO<Community> layuiDataVO = new LayuiDataVO<>();

        layuiDataVO.setCount( pageInfo.getTotal());
        layuiDataVO.setCode(0);
        layuiDataVO.setData(list);

        return layuiDataVO;
    }


    /**
    * 查询车辆信息
    * @title findAllCarPage
    * @param currentPage 当前页
    * @param pageSize 每页数据
    * @param car 车辆信息
    * @return com.woniu.sharepark.vo.LayuiDataVO<com.woniu.sharepark.leasesystem.tenant.pojo.Car>
    * @author xin
    * @date 2019/6/4 20:22
    */
    @RequestMapping("/car")
    @ResponseBody
    public LayuiDataVO<Car> findAllCarPage(@RequestParam("page") Integer currentPage,@RequestParam("limit") Integer pageSize, Car car){

        List<Car> list=carService.findAllCarPage(currentPage,pageSize,car);
        PageInfo<Car> pageInfo=new PageInfo<>(list);


        // 设置返回的表格数据
        LayuiDataVO<Car> layuiDataVO = new LayuiDataVO<>();

        layuiDataVO.setCount( pageInfo.getTotal());
        layuiDataVO.setCode(0);
        layuiDataVO.setData(list);


        return layuiDataVO;

    }

    /**
    * 查询车位信息
    * @title findAllCarParkPage
    * @param currentPage 当前页
    * @param pageSize 每页数据
    * @param carPark 车位信息
    * @return com.woniu.sharepark.vo.LayuiDataVO<com.woniu.sharepark.leasesystem.lessor.pojo.CarPark>
    * @author xin
    * @date 2019/6/4 20:22
    */
    @RequestMapping("/carPark")
    @ResponseBody
    public LayuiDataVO<CarPark> findAllCarParkPage(@RequestParam("page") Integer currentPage,@RequestParam("limit") Integer pageSize,CarPark carPark){

        List<CarPark> list=carParkService.findAllCarParkPage(currentPage,pageSize,carPark);
        PageInfo<CarPark> pageInfo=new PageInfo<>(list);

        // 设置返回的表格数据
        LayuiDataVO<CarPark> layuiDataVO = new LayuiDataVO<>();

        layuiDataVO.setCount( pageInfo.getTotal());
        layuiDataVO.setCode(0);
        layuiDataVO.setData(list);

        return layuiDataVO;
    }

    /**
    * 平台方下架车位
    * @title removeCarPark
    * @param id 车位id
    * @return com.woniu.sharepark.common.JsonResult
    * @author xin
    * @date 2019/6/12 11:31
    */
    @RequestMapping("/removeCarPark")
    @ResponseBody
    public JsonResult removeCarPark(Integer id){

        CarPark carPark=new CarPark();
        carPark.setId(id);
        carPark.setcState("下架");

            if (carParkService.updateByPrimaryKeySelective(carPark)>0){
                return JsonResult.ok("已下架");
            }else {
                return JsonResult.error("下架失败");
            }
    }

    /**
    * 处理投诉信息
    * @title setComplaint
    * @param id 投诉信息id
    * @return com.woniu.sharepark.common.JsonResult
    * @author xin
    * @date 2019/6/12 16:44
    */
    @RequestMapping("/setComplaint")
    @ResponseBody
    @BackgroundLogAnnotation(operate = "处理投诉")
    public JsonResult setComplaint(Integer id){

        return complaintService.disposeComplaint(id);
    }

    /**
    * 查询所有投诉信息
    * @title findAllCarParkPage
    * @param currentPage 当前页
    * @param pageSize 每页数量
    * @param complaintVO 投诉信息条件
    * @return com.woniu.sharepark.vo.LayuiDataVO<com.woniu.sharepark.leasesystem.tenant.pojo.Complaint>
    * @author xin
    * @date 2019/6/12 14:05
    */
    @RequestMapping("/allComplaint")
    @ResponseBody
    public LayuiDataVO<Complaint> findAllCarParkPage(@RequestParam("page") Integer currentPage, @RequestParam("limit") Integer pageSize, ComplaintVO complaintVO){

        List<Complaint> list=complaintService.allComplaints(currentPage,pageSize,complaintVO);


        PageInfo<Complaint> pageInfo=new PageInfo<>(list);

        // 设置返回的表格数据
        LayuiDataVO<Complaint> layuiDataVO = new LayuiDataVO<>();

        layuiDataVO.setCount( pageInfo.getTotal());
        layuiDataVO.setCode(0);
        layuiDataVO.setData(list);

        return layuiDataVO;
    }
    /**
    *@Description: 平台方审核车位信息
    *@Param:
    *@return 影响行数
    *@Author zy
    *@date: 2019/6/10
    */

    @RequestMapping("/check")
    @ResponseBody
    @BackgroundLogAnnotation(operate = "平台审核车位")
    public JsonResult checkCarpark(String param,String phone){
        Integer id = Integer.parseInt(param);

        try {
            int result = carParkService.updateCarpark(id);
            //审核通过之后调用短信接口发短信通知车位主
          VerificationCodeUtil.sendSms(phone,VerificationCodeUtil.getMsgCode());


            return JsonResult.ok("审核通过");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("审核通过失败");
        }
    }

    @RequestMapping("/order")
    @ResponseBody
    public LayuiDataVO<OrderDTO> findOrders(@RequestParam("page")Integer currentPage,
                                            @RequestParam("limit")Integer pageSize,
                                            OrderDTO orderDTO){




        List<OrderDTO> list = communityService.findAllOrder(currentPage,pageSize,orderDTO);
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(list);
        //设置返回的表格数据

        return new LayuiDataVO<>(0,"",pageInfo.getTotal(),list);

    }



    @GetMapping("count")
    @ResponseBody
    public Map<String,Integer> findCount(){
        Map<String,Integer> map=new HashMap<String,Integer>(16);

        Integer count1=0;
        Integer count2=0;
        Integer count3=0;
        Integer count4=0;

        count1=userService.findAllCount();
        count2=carService.findAllCount();
        count3=carParkService.findAllCount();
        count4=communityService.findAllCount();


        map.put("userCount",count1);
        map.put("carCount",count2);
        map.put("carParkCount",count3);
        map.put("comCount",count4);

        return map;
    }

    /**
    * 获取平台分成
    * @title getMoney
    * @return double
    * @author xin
    * @date 2019/6/20 23:09
    */
    @RequestMapping("/getMoney")
    @ResponseBody
    public double getMoney(){

        List<OrderForm> list=orderFormService.findAllOrder();
        double money=0.0;


        for (OrderForm o:list
        ) {
            money+=o.getoTotalmoney();
        }
        double scale=0.0;
        scale=scaleService.findScale().getsSystem();
        money=money*scale;

        BigDecimal bg = new BigDecimal(money).setScale(2, RoundingMode.UP);

        return bg.doubleValue();

    }


    /**
    * 统计数据
    * @title countData
    * @return java.util.Map<java.lang.String,java.lang.Integer>
    * @author xin
    * @date 2019/6/20 23:10
    */
    @RequestMapping("/getTotal")
    @ResponseBody
    public Map<String,Integer> countData(){

        Map<String,Integer> map=new HashMap<String, Integer>(16);

        List<CarPark> parkList=new ArrayList<>();

        CarPark carPark=new CarPark();
        CarPark carPark1=new CarPark();


        //待审核车位
        carPark.setcStatus("物业通过");
        parkList=carParkService.findCarParkSelective(carPark);
        map.put("unCheckParkCount",parkList.size());

        //已出租车位
        carPark1.setcState("已租");
        parkList=carParkService.findCarParkSelective(carPark1);
        map.put("busyParkCount",parkList.size());


        // 所有已完成订单
        List<OrderForm> orderFormList=orderFormService.findAllOrder();
        map.put("orderCount",orderFormList.size());

        //查询所有投诉

        ComplaintVO complaintVO=new ComplaintVO();
        Integer num=complaintService.countOfComplaints(complaintVO);
      map.put("complaintCount",num);


        return map;
    }

    /**
    * 统计注册小区在全国分布
    * @title getComCount
    * @return java.util.Map<java.lang.String,java.lang.Integer>
    * @author xin
    * @date 2019/6/21 11:19
    */
    @RequestMapping("/getComCount")
    @ResponseBody
    public Map<String,Integer> getComCount(){

        Map<String,Integer> map=new HashMap<String, Integer>(16);

        List<Community> communities=communityService.findAllCommunityMap();

        Integer countBJ=0;
        Integer countSH=0;
        Integer countGD=0;
        Integer countSC=0;
        Integer countCQ=0;
        Integer countJS=0;
        Integer countSD=0;
        Integer countHN=0;
        Integer countHB=0;
        Integer countTJ=0;
        Integer countFJ=0;
        Integer countHEB=0;

        for (Community c:communities
             ) {

           switch (c.getComProvince()){

               case "北京":countBJ++;break;
               case "上海":countSH++;break;
               case "广东":countGD++;break;
               case "天津":countTJ++;break;
               case "四川":countSC++;break;
               case "福建":countFJ++;break;
               case "重庆":countCQ++;break;
               case "湖南":countHN++;break;
               case "湖北":countHB++;break;
               case "河北":countHEB++;break;
               case "山东":countSD++;break;
               case "江苏":countJS++;break;

                default:break;
           }



        }


        map.put("countBJ",countBJ);
        map.put("countSH",countSH);
        map.put("countGD",countGD);
        map.put("countTJ",countTJ);
        map.put("countSC",countSC);
        map.put("countFJ",countFJ);
        map.put("countCQ",countCQ);
        map.put("countHN",countHN);
        map.put("countHB",countHB);
        map.put("countHEN",countHEB);
        map.put("countSD",countSD);
        map.put("countJS",countJS);

        return map;
    }

}
